package com.example.common.advice;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.example.common.utils.AesUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.IOUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.MethodParameter;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpInputMessage;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.servlet.mvc.method.annotation.RequestBodyAdvice;

import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.Type;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;

@Slf4j
@ControllerAdvice
public class requestAdvice implements RequestBodyAdvice {

    @Value("${config.encrypt.enable}")
    private boolean encrypt;

    @Value("${config.encrypt.exclude}")
    private String exclude;

    @Override
    public boolean supports(MethodParameter methodParameter, Type targetType, Class<? extends HttpMessageConverter<?>> converterType) {
        return true;
    }

    @Override
    public HttpInputMessage beforeBodyRead(HttpInputMessage inputMessage, MethodParameter parameter, Type targetType, Class<? extends HttpMessageConverter<?>> converterType) throws IOException {
        String requestBody = IOUtils.toString(inputMessage.getBody(), StandardCharsets.UTF_8);
        JSONObject requestBodyJson = JSONObject.parseObject(requestBody);
        log.info("获取请求体中的数据为: {}", requestBodyJson);
        String data = requestBodyJson.getString("data");
        String aesDecryptData = null;
        try {
            if(encrypt) {
                aesDecryptData = AesUtil.decrypt(data);
            }
        } catch (Exception e) {
            log.error("解密失败");
        }
        // 将解密后的数据放回请求体中
        HashMap<String, Object> hashMap = JSON.parseObject(aesDecryptData, HashMap.class);
        requestBody = JSON.toJSONString(hashMap);
        if(encrypt) {
            log.info("解密后请求体中的数据为: {}", requestBody);
        }
        InputStream inputStream = IOUtils.toInputStream(requestBody, StandardCharsets.UTF_8);
        // 返回数据
        return new HttpInputMessage() {
            @Override
            public InputStream getBody() throws IOException {
                return inputStream;
            }
            @Override
            public HttpHeaders getHeaders() {
                return inputMessage.getHeaders();
            }
        };
    }

    @Override
    public Object afterBodyRead(Object body, HttpInputMessage inputMessage, MethodParameter parameter, Type targetType, Class<? extends HttpMessageConverter<?>> converterType) {
        return body;
    }

    @Override
    public Object handleEmptyBody(Object body, HttpInputMessage inputMessage, MethodParameter parameter, Type targetType, Class<? extends HttpMessageConverter<?>> converterType) {
        return body;
    }
}
